package com.wisemoresystem.seal.action_app;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.wisemoresystem.seal.web.SessionDistributed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.UserService;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.SessionVal;
import com.wisemoresystem.seal.web.WebTools;
/**
 * 客户端登陆
 * @author Administrator
 *
 */
@Controller("LoginApp")
@Scope("prototype")
public class LoginAction extends ProjBaseAction {

	@Autowired
	private UserService userService;
	/**
	 * 客户端登陆
	 * @param user
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/login_app")
	@ResponseBody
	public t_user login_app(@RequestBody t_user user) throws IOException {
		System.err.println(user + "============");

		Map<String, String> map = new HashMap<String, String>();
		map.put("result", "false");

		// t_user user = userService.selectUserByUnameAndPwd(uname, pwd);
		if (user == null) {
			map.put("result", "false");
		} else {
			map.put("result", "true");
			SessionDistributed.setAttribute(SessionVal.login_user, user, resp);
			// req.getSession().setAttribute(SessionVal.login_user, user);
		}
		t_user u = new t_user();
		u.setUname("kakaxi");

		return u;
	}
	/**
	 * 测试session
	 * @throws IOException
	 */
	@RequestMapping("/test_session")
	public void test_session() throws IOException {

		Map<String, String> map = new HashMap<String, String>();
		map.put("result", "false");

		t_user user = WebTools.getCurrentUser();
		if (user == null) {
			map.put("result", "sorry,session is null.");
		} else {
			map.put("result", "yes!session is well!");
		}

		resp.getWriter().print(JsonTools.toJson(map));
		resp.getWriter().flush();

	}

}
